package com.killer.redissessiondemo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器，没有登录则跳转到登录页面
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        //获取是否有loginUser
        Object user = session.getAttribute("loginUser");
        if (user != null) {
            return true;
        }
        response.sendRedirect("/login.html");
        return false;
    }
}
